// import { ref } from "vue";
// import { remove, isNull, hasIn } from 'lodash';

import { isArray, isNil } from "lodash";
import { FormFieldItem, QueryItem, TableColumn } from "./types/app.js";
import { BaseModel } from "./types/model.js";
import { dayjs } from "element-plus";
import { checkCntrNo } from "./cfstools.js";
import {  utils, writeFile } from 'xlsx';
// import { tableMaxHeightAdjust } from "./const";
// 从ISO datetime字符串格式化成 本地时间字符串
export function getFormatDate(d: string): string {
    const date = new Date(d.replace('T', ' ').replace('Z', ''));
    const year = date.getFullYear();
    let month: number | string = date.getMonth() + 1;
    let day: number | string = date.getDate();
    let hour: number | string = date.getHours();
    let minutes: number | string = date.getMinutes();
    let seconds: number | string = date.getSeconds();
    month = (month < 10) ? '0' + month : month;
    day = (day < 10) ? '0' + day : day;
    hour = (hour < 10) ? '0' + hour : hour;
    minutes = (minutes < 10) ? '0' + minutes : minutes;
    seconds = (seconds < 10) ? '0' + seconds : seconds;
    const currentDate = year + "-" + month + "-" + day
        + " " + hour + ":" + minutes + ":" + seconds;
    return currentDate;
}
// 初始化一个对象
export function initObjectProperty(paramData: BaseModel) {
    for (const key in paramData) {
        // eslint-disable-next-line no-prototype-builtins
        if (paramData.hasOwnProperty(key)) {
            if (typeof paramData[key] === 'string') {
                paramData[key] = undefined;
            } else if (typeof paramData[key] === 'number') {
                paramData[key] = undefined;
            } else if (typeof paramData[key] === 'boolean') {
                paramData[key] = undefined;
            } else if (isArray(paramData[key])) {
                paramData[key] = [];
            } else {
                paramData[key] = undefined;
            }
        }
    }
}
// 从tablecolumn数组生成 form表单字段
export function pickFormFromTable(source: TableColumn[]): FormFieldItem[] {
    const rarr = Array<FormFieldItem>();
    for (let index = 0; index < source.length; index++) {
        const element = source[index];
        if (element.noFormField) {
            continue;
        }
        const r: FormFieldItem = { };
        r.field = element.formFieldProp ?? element.prop;
        r.label = element.label;
        r.type = element.formFieldItemType;
        r.visable = element.formFiledItemVisable;
        r.optionalValues = element.optionalValues;
        r.cascaderFields = element.cascaderFields;
        r.options = element.formOption;
        rarr.push(r);
    }
    return rarr;
}
// 从TableColumn数组生成通用查询表单字段
export function pickQueryFromTable(source: TableColumn[]): QueryItem[] {
    const rarr = Array<QueryItem>();
    for (let index = 0; index < source.length; index++) {
        const element = source[index];
        if (element.noquery) {
            continue;
        }
        const r: QueryItem = { 
            fieldCod: '',
            fieldName: '',
            fieldType: ''
        };
        r.fieldCod = (element.queryFieldProp ?? element.prop) as string;
        r.fieldName = element.label as string;
        r.fieldType = element.formFieldItemType!;
        r.treeData = element.treeData;
        r.optionalValues = element.optionalValues;
        rarr.push(r);
    }
    return rarr;
}
// 代码 ---> 值  {label: string , value: string | number }
export function transFormatter(val: number | string, data: any) {
    for (let index = 0; index < data.length; index++) {
        const element = data[index];
        if (element.value == val) {
            return element.label;
        }
    }
    return val;
}
// key ----> 值 从对象数组中 按指定key值返回 另一个指定key的值
export function getObjectKeyValue(sourceKey: string, val: number | string, data: Array<any>, targetKey: string) {
    for (let index = 0; index < data.length; index++) {
        const element = data[index];
        if (element[sourceKey] === val) {
            return element[targetKey];
        }
    }
    return null;
}
// key ----> 对象 从对象数组中 按指定key值返回对象
export function getObjectFromArray(sourceKey: string, val: number | string, data: Array<any>) {
    for (let index = 0; index < data.length; index++) {
        const element = data[index];
        if (element[sourceKey] === val) {
            return element;
        }
    }
    return null;
}
//格式化日期时间
export function formatDateTime(_row: any, column: any, cellValue: any) {
    if (isNil(cellValue)) {
        return '';
    }
    return dayjs(cellValue).format('YYYY-MM-DD HH:mm:ss');
}
// 格式化日期
export function formatDate(_row: any, column: any, cellValue: any) {
    if (isNil(cellValue)) {
        return '';
    }
    return dayjs(cellValue).format('YYYY-MM-DD');
}


// 箱号正则校验rule
export function validateCntr(_rule: any, value: any, callback: any) {
    const regex = new RegExp(
        /^([A-Z]{3}[U]\d{6}[0-9])$/gm
    );
    if (!regex.test(value)) {
        callback(new Error('非法箱号-格式错误'));
    }
    if (!checkCntrNo(value)) {
        callback(new Error('非法箱号-箱号规则错误'));
    }
    callback();
}
// 箱号正则校验函数
export function validateCntrFunc(cntr: string): boolean {
    const regex = new RegExp(
        /^([A-Z]{3}[U]\d{6}[0-9])$/gm
    );
    if (!regex.test(cntr)) {
        return false;
    }
    if (!checkCntrNo(cntr)) {
        return false;
    }
    return true;
}
export function exportExcel(id: string) {
    const table = document.getElementById(id);
    const wbb = utils.table_to_book(table);
    writeFile(wbb, 'data.xlsx');
}